define([
	'jquery',
	'underscore',
	'backbone',
	'catjs/util',
	],function($,_,Backbone,util){
	var module = {};


	//图书馆视图
	var LibView = Backbone.View.extend({

		el : $('#lib-building'),
		template : $('#tplLibBooks').text(),
		events : {
			'click .lib-leave' : 'leave',			
			'click .lib-dosearch' : 'search',
			'click .lib-research' : 'research',
		},

		render : function(){
			var self = this;
			self.$el.fadeIn();
			$('#viewport').fadeOut();
		},
		leave : function(){
			var self = this;
			self.$el.fadeOut();
			$('#viewport').fadeIn();
		},
		search : function(){
			var self = this;
			var keyword = self.$el.find('textarea.lib-keyword').val();
			self.$el.find('textarea.lib-keyword').val('');
			self.$el.find('.lib-search').fadeOut();
			self.$el.find('.lib-result').fadeIn();
			$.ajax({
				url : '/Library/Default',
				type : 'GET',
				data : {
					keyword : keyword,
				},
				success : function(json){
					var books = JSON.parse(json);
					books.forEach(function(book){
						book.title = util.strSnip(book.title,20);
						book.author = util.strSnip(book.author,20);
					});
					self.$el.find('.lib-result').html(
						util.ejs.render(self.template,{books:books})
					);
				},
			});
		},
		research : function(){
			var self = this;
			self.$el.find('.lib-search').fadeIn();
			self.$el.find('.lib-result').fadeOut();
		},
	});
	var libView = new LibView;

	//进入图书馆
	module.enter = function(){
		libView.render();
	};

	return module;
});
